<?php

namespace app\service\category;

use app\service\admin\RolePurviewLogic;
use app\service\base\BaseLogic;
use think\Exception;

class CategoryLogic extends BaseLogic
{
    public function treeList()
    {
        $arrData = (new CategoryModel())->getAll();
        $arrData = $this->tree($arrData);
        return $arrData;
    }

    public function treeListByIds($arrIds)
    {
        $arrData = (new CategoryModel())->getAllByIds($arrIds);
        $arrData = $this->tree($arrData);
        return $arrData;
    }

    public function add($name, $pid, $sort)
    {
        if (!$this->check($name)) {
            throw new Exception('已存在同名分类');
        }
        $arrData = [
            'name' => $name,
            'pid'  => $pid,
            'sort' => $sort,
        ];
        return (new CategoryModel())->add($arrData);
    }

    public function edit($id, $name, $pid, $sort)
    {
        $arrData = [
            'name' => $name,
            'pid' => $pid,
            'sort' => $sort,
        ];
        return (new CategoryModel())->edit($id, $arrData);
    }

    public function del($id)
    {
        if (empty($id)) {
            throw new \Exception('更新参数错误');
        }
        return (new CategoryModel())->delById($id);
    }

    public function check($name)
    {
        $arrWhere = [];
        $arrWhere[] = ['name', '=', $name];
        $arrData = (new CategoryModel())->getRow($arrWhere);
        return !empty($arrData) ? false : true;
    }

    private function tree($arrData, $pid = 0, $level = 0)
    {
        if (empty($arrData)) {
            return [];
        }
        $arrReturn = [];
        foreach ($arrData as $k => $arrVal) {
            if ($arrVal['pid'] == $pid) {
                $arrTmp = $arrVal;
                $arrTmp['level'] = $level;
                $arrTmp['sub'] = $this->tree($arrData, $arrVal['id'], $level + 1);
                $arrReturn[] = $arrTmp;
            }
        }
        return $arrReturn;
    }

    public function getRowById($id)
    {
        $arrWhere = [];
        $arrWhere['id'] = $id;
        return (new CategoryModel())->getRow($arrWhere);
    }

    public function getList()
    {
        $arrPurview = (new CategoryModel())->getAll();
        return $this->formatTreeData($arrPurview);
    }
    
    public function formatTreeData($arrAdminPurview, $arrSelect = [])
    {
        if (empty($arrAdminPurview)) {
            return [];
        }
        $arrReturnAdminPurview = [];
        foreach ($arrAdminPurview as $key => $item) {
            $tmp = [];
            $tmp['id'] = $item['id'];
            $tmp['pId'] = $item['pid'];
            $tmp['name'] = $item['name'];
            if ($arrSelect && in_array($tmp['id'], $arrSelect)) {
                $tmp['checked'] = true;
            } else {
                $tmp['checked'] = false;
            }
            $arrReturnAdminPurview[$key] = $tmp;
        }
        return $arrReturnAdminPurview;
    }

    public function getAll($roleId)
    {
        $arrRolePurview = RolePurviewLogic::getInstance()->getListByRoleId($roleId);
        $arrRolePurview = array_column($arrRolePurview, 'admin_purview_id');

        $arrData = (new CategoryModel())->getAll();
        $arrData = purview_tree_no_sub($arrData);
        $arrData = $this->formatTreeData($arrData, $arrRolePurview);
        return array_values($arrData);
    }

    public function privilege($roleId, $arrPurviewIds)
    {
        // 现有权限
        $arrHavePurview = RolePurviewLogic::getInstance()->getListByRoleId($roleId);
        $arrHavePurview = array_column($arrHavePurview, 'admin_purview_id');

        // 没有再次勾选的要删掉
        $arrDel = array_diff($arrHavePurview, $arrPurviewIds);
        if ($arrDel) {
            RolePurviewLogic::getInstance()->delByIds($roleId, $arrDel);
        }

        // 新勾选的要增加
        $arrAdd = array_diff($arrPurviewIds, $arrHavePurview);
        if ($arrAdd) {
            $arrInsert = [];
            foreach ($arrAdd as $id) {
                $arrInsert[] = ['admin_role_id' => $roleId, 'admin_purview_id' => $id];
            }
            RolePurviewLogic::getInstance()->addMulti($arrInsert);
        }
        return true;
    }

    public function getAllCategory()
    {
        return (new CategoryModel())->getAll();
    }
}